package network;

import gui.MainPanel;
import gui.SuperUserPanel;

import java.util.concurrent.ExecutionException;

import javax.swing.JPanel;
import javax.swing.SwingWorker;

import data.GetOrderListResponse;


public class GetOrderListWorker extends SwingWorker<GetOrderListResponse, Object> {

	private JPanel mReturnPanel = null;
	private boolean mOnlySubmitted = false;
	
	public GetOrderListWorker(JPanel returnPanel) {
		mReturnPanel = returnPanel;
		mOnlySubmitted = returnPanel instanceof SuperUserPanel;
	}
	
	@Override
	protected GetOrderListResponse doInBackground() throws Exception {
		return mOnlySubmitted ? NetworkManager.getInstance().getSubmittedOrders() 
				: NetworkManager.getInstance().getOrderList();
	}
	
	@Override
	protected void done() {
		GetOrderListResponse status = null;
		try {
			status = get();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (mOnlySubmitted)
				((SuperUserPanel)mReturnPanel).ordersObtained(status);
			else
				((MainPanel)mReturnPanel).showOrderList(status);
		}
		super.done();
	}

}
